package com.study.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/4/15 12:27 上午
 */
public final class JackJsonUtil {
    private static final ObjectMapper JACKJSON_MAPPER = new ObjectMapper();

    public static String toJSONString(Object obj) {
        try {
            return JACKJSON_MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException var2) {
            //FrameLogs.framework.error(var2, new Pair[]{Pair.of("clazz", "JackJsonUtil"), Pair.of("method", "toJSONString"), Pair.of("status", "exceptions")});
            return "";
        }
    }

    public static <T> T parse2Obj(String message, Class<T> clazz) {
        try {
            return JACKJSON_MAPPER.readValue(message, clazz);
        } catch (IOException var3) {
            //FrameLogs.framework.error(var3, new Pair[]{Pair.of("clazz", "JackJsonUtil"), Pair.of("method", "parse2Obj"), Pair.of("status", "exceptions")});
            return null;
        }
    }

    public static <T> List<T> parse2List(String message, Class<T> clazz) {
        try {
            if (StringUtils.isEmpty(message)) {
                return null;
            } else {
                message = message.trim();
                if (!message.startsWith("[")) {
                    message = "[" + message;
                }

                if (!message.endsWith("]")) {
                    message = message + "]";
                }

                return (List)JACKJSON_MAPPER.readValue(message, TypeFactory.defaultInstance().constructCollectionType(ArrayList.class, clazz));
            }
        } catch (IOException var3) {
            //FrameLogs.framework.error(var3, new Pair[]{Pair.of("clazz", "JackJsonUtil"), Pair.of("method", "parse2List"), Pair.of("status", "exceptions")});
            return null;
        }
    }

    public static <T> Map<String, T> parse2Map(String message, Class<T> clazz) {
        try {
            if (StringUtils.isEmpty(message)) {
                return null;
            } else {
                message = message.trim();
                if (!message.startsWith("{")) {
                    message = "{" + message;
                }

                if (!message.endsWith("}")) {
                    message = message + "}";
                }

                return (Map)JACKJSON_MAPPER.readValue(message, TypeFactory.defaultInstance().constructMapType(HashMap.class, String.class, clazz));
            }
        } catch (IOException var3) {
            //FrameLogs.framework.error(var3, new Pair[]{Pair.of("clazz", "JackJsonUtil"), Pair.of("method", "parse2Map"), Pair.of("status", "exceptions")});
            return null;
        }
    }

    private JackJsonUtil() {
    }

    static {
        JACKJSON_MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        JACKJSON_MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        JACKJSON_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        JACKJSON_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
    }
}